package com.compass.tumor.splash

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.compass.doctor.dialog.PrivacyDialog
import com.compass.doctor.ui.MainActivity
import com.compass.doctor.ui.login.LoginActivity
import com.compass.framework.ext.countDownCoroutines
import com.compass.framework.helper.CompassAppHelper
import com.compass.framework.manager.CommonManager
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.utils.StatusBarSettingHelper
import com.compass.tumor.CompassApplication
import com.compass.tumor.databinding.ActivitySplashBinding
import com.compass.tumor.push.PushMessageHandler
import com.heytap.msp.push.HeytapPushManager
import com.hihonor.push.sdk.HonorPushClient
import com.huawei.hms.support.common.ActivityMgr
import com.netease.yunxin.kit.corekit.im.IMKitClient
import com.netease.yunxin.kit.corekit.im.repo.SettingRepo
import com.netease.yunxin.kit.corekit.im.utils.IMKitUtils
import com.netease.yunxin.kit.locationkit.LocationKitClient
import com.vivo.push.PushClient
import com.vivo.push.util.VivoPushException


/**
 * Description： 启动页
 * Date:2023/11/6 15:13
 */
class SplashActivity : AppCompatActivity() {

    private lateinit var activitySplashBinding: ActivitySplashBinding
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        StatusBarSettingHelper.setStatusBarTranslucent(this)
        StatusBarSettingHelper.statusBarLightMode(this@SplashActivity, true)
        activitySplashBinding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(activitySplashBinding.root)
        CompassApplication.setColdStart(true)

        if (CommonManager.getPrivacyStatus()) {
            //倒计时
            countDownCoroutines(1, lifecycleScope, onTick = {

            }) {
                goToNext()
            }
        } else {
            showPrivacyDialog()
        }
    }


    private fun goToNext() {

        if (UserInfoManager.isLogin()){
            MainActivity.start(this,0)
        }else{
            //注意这里需要调用无参的分步初始化的方法，必须要在主进程中调用。
            IMKitClient.initSDK()
            //初始化推送
            initPushSDK()
            LoginActivity.start(this)
        }
        finish()
    }

    private fun initPushSDK(){
        if (IMKitUtils.isMainProcess(this)) {
            // 位置消息模块需要初始化
            LocationKitClient.init(this)
            //huawei push
            ActivityMgr.INST.init(CompassAppHelper.getApplication())
            //honor push
            HonorPushClient.getInstance().init(this,true)

            //oppo push
            HeytapPushManager.init(this, true)
            try {
                //vivo push
                PushClient.getInstance(this).initialize()
            } catch (e: VivoPushException) {
                e.printStackTrace()
            }
            IMKitClient.toggleNotification(SettingRepo.isPushNotify())
        }
        IMKitClient.registerMixPushMessageHandler(PushMessageHandler())
    }
    /**
     * 隐私协议弹窗
     */
    private fun showPrivacyDialog() {
        PrivacyDialog.Builder(this@SplashActivity)
            .setonCancelListener {
                it?.dismiss()
                finish()
            }
            .setonConfirmListener {
                CommonManager.setPrivacyStatus(true)
                goToNext()
                it?.dismiss()
            }.setCancelable(false)
            .setCanceledOnTouchOutside(false)
            .create().show()
    }



}
